package setting

import (
	"github.com/go-ini/ini"
	"log"
	"time"
)

var (
	Cfg *ini.File

	RunMode string

	HttpPort int

	ReadTimeOut  time.Duration
	WriteTimeOut time.Duration

	JwtSecret string
)

func init() {
	var err error
	Cfg, err = ini.Load("conf/app.ini")

	if err != nil {
		log.Fatalf("Fail to parse app.ini, %v", err)
	}
	LoadBase()
	loadServer()
	loadApp()

}

func LoadBase() {
	RunMode = Cfg.Section("").Key("RUN_MODE").MustString("release")
}

func loadServer() {
	sec, err := Cfg.GetSection("server")
	if err != nil {
		log.Printf("Fail to get section server: %v", err)
	}

	HttpPort = sec.Key("HTTP_PORT").MustInt(8080)
	ReadTimeOut = time.Duration(sec.Key("READ_TIMEOUT").MustInt(60)) * time.Second
	WriteTimeOut = time.Duration(sec.Key("WRITE_TIMEOUT").MustInt(60)) * time.Second
}

func loadApp() {
	sec, err := Cfg.GetSection("app")
	if err != nil {
		log.Fatalf("Faile to get section app : %v", err)
	}

	JwtSecret = sec.Key("JWT_SECRET").MustString("!@#$%^&*()")
}
